package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.RechargeUseDto;
import io.geekidea.boot.shop.entity.RechargeUse;
import io.geekidea.boot.shop.mapper.RechargeUseMapper;
import io.geekidea.boot.shop.query.app.AppRechargeUseQuery;
import io.geekidea.boot.shop.query.web.RechargeUseQuery;
import io.geekidea.boot.shop.service.base.api.RechargeUseService;
import io.geekidea.boot.shop.vo.app.AppRechargeUseVo;
import io.geekidea.boot.shop.vo.web.RechargeUseVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class RechargeUseServiceImpl extends ServiceImpl<RechargeUseMapper, RechargeUse> implements RechargeUseService {

    @Autowired
    private RechargeUseMapper rechargeUseMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addRechargeUse(RechargeUseDto dto) {
        RechargeUse rechargeUse = new RechargeUse();
        BeanUtils.copyProperties(dto, rechargeUse);
        return save(rechargeUse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateRechargeUse(RechargeUseDto dto) {
        Integer id = dto.getId();
        RechargeUse rechargeUse = getById(id);
        if (rechargeUse == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, rechargeUse);
        return updateById(rechargeUse);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteRechargeUse(Integer id) {
        return removeById(id);
    }

    @Override
    public RechargeUseVo getRechargeUseById(Integer id) {
        return rechargeUseMapper.getRechargeUseById(id);
    }

    @Override
    public Paging<RechargeUseVo> getRechargeUsePage(RechargeUseQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<RechargeUseVo> list = rechargeUseMapper.getRechargeUsePage(query);
        Paging<RechargeUseVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppRechargeUseVo getAppRechargeUseById(Integer id) {
        return rechargeUseMapper.getAppRechargeUseById(id);
    }

    @Override
    public Paging<AppRechargeUseVo> getAppRechargeUsePage(AppRechargeUseQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppRechargeUseVo> list = rechargeUseMapper.getAppRechargeUsePage(query);
        Paging<AppRechargeUseVo> paging = new Paging<>(list);
        return paging;
    }

}
